package com.itheima.imclient100.presenter.impl;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SignUpCallback;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.itheima.imclient100.presenter.RegistPresenter;
import com.itheima.imclient100.utils.ThreadUtils;
import com.itheima.imclient100.view.RegistView;

/**
 * Created by fullcircle on 2017/7/2.
 */

public class RegistPresenterImpl implements RegistPresenter {
    private RegistView registView;

    public RegistPresenterImpl(RegistView registView) {
        this.registView = registView;
    }

    @Override
    public void regist(final String username, final String passWord) {
        //① 到leanCloud注册用户  ORM  AVUser对象 实际上对应的 服务端 _User表里的一条数据
        final AVUser user = new AVUser();
        user.setUsername(username);
        user.setPassword(passWord);
        //在子线程中注册用户到leanCloud
        user.signUpInBackground(new SignUpCallback() {
            @Override
            public void done(final AVException e) {
                if(e == null){
                  //说明注册成功
                    //② 如果注册成功 继续到环信注册
                    ThreadUtils.runOnSubThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                EMClient.getInstance().createAccount(username,passWord);//在当前线程执行
                                //③ 到环信注册用户 如果成功 整个注册就认为是成功的
                                ThreadUtils.runOnMainThread(new Runnable() {
                                    @Override
                                    public void run() {
                                      registView.onGetRegistResult(true,null);
                                    }
                                });
                            } catch (final HyphenateException e1) {
                                e1.printStackTrace();
                                ThreadUtils.runOnMainThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        //如果注册失败 还需要删除之前leanCloud注册成功的用户 返回注册失败
                                        registView.onGetRegistResult(false,e1.getMessage());
                                        try {
                                            //还需要删除之前leanCloud注册成功的用户
                                            user.delete();
                                        } catch (AVException e2) {
                                            e2.printStackTrace();
                                        }
                                    }
                                });

                            }
                        }
                    });


                }else{
                    //说明leanCloud注册失败
                   // 如果注册失败 直接返回注册失败的信息
                    registView.onGetRegistResult(false,e.getMessage());
                }
            }
        });



    }
}
